#include "Solver.h"

#include <time.h>

//#define DEBUG

Solver::Solver(Instance* aInstance)
  : pInstance(aInstance)
  , pStates()
  , pState(aInstance)
  , pCounter(0)
  , pElapsed(0)
{
}

Solver::~Solver() {
}

State& Solver::solve() {
  pElapsed = time(NULL);

  for(uint16_t i=2;i<=pInstance->getRegularity() + 1;++i) {
    pStates.push(State(pInstance, i));

    while (!pStates.empty()) {
      pState = pStates.top(); pStates.pop();

      if (pState.isFinal()) {
        pElapsed = time(NULL) - pElapsed;
        return pState;
      }

      for(uint16_t j = pState.getMaxColor();j>=1;--j) {
        if (!pState.willBeValid(j)) continue;
        
        ++pCounter;
        
        State kState = State(pState);
        kState.nextNode() = j;

        pStates.push(kState);
      }
    }
  }

  pElapsed = time(NULL) - pElapsed;
  return pState;
}

time_t Solver::getElapsed() {
  return pElapsed;
}

long Solver::getCount() {
  return pCounter;
}

// vim: ts=2 fdm=marker syntax=cpp expandtab sw=2
